//
//  ViewController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/1.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "MainController.h"
#import "WJNavigationController.h"
#import "WJMainItemCell.h"
#import "WJMainGroupView.h"
#import "WJMainItem.h"
#import "WJMainGroup.h"


@interface MainController () <UITableViewDelegate, UITableViewDataSource, WJMainGroupViewDelegate>

@property (nonatomic, strong) NSArray *groupArray;

@end

@implementation MainController

//是否显示状态条
- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //第一行cell的高度
    self.tableView.rowHeight = 65;
    //第一组头部控件的高度
    self.tableView.sectionHeaderHeight = 45;
}

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO];
}

-(NSArray *)groupArray {
    if (_groupArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"home_data.plist" ofType:nil];
        NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *groupArray = [NSMutableArray array];
        for (NSDictionary *dic in dicArray) {
            WJMainGroup *group = [WJMainGroup groupWithDic:dic];
            [groupArray addObject:group];
        }
        
        _groupArray = groupArray;
    }
    return _groupArray;
}

#pragma 数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    WJMainGroup *group = self.groupArray[section];
    return group.isOpened ? group.itemArray.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //创建cell
    WJMainItemCell *cell = [WJMainItemCell cellWithTableView:tableView];
    //设置cell的数据
    WJMainGroup *group = self.groupArray[indexPath.section];
    cell.item = group.itemArray[indexPath.row];
    
    return cell;
}

//返回需要显示的头部标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //创建头部控件
    WJMainGroupView *groupView = [WJMainGroupView groupViewWithTableView:tableView];
    groupView.delegate = self;
    //给group设置数据
    groupView.group = self.groupArray[section];
    
    return groupView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WJMainGroup *group = self.groupArray[indexPath.section];
    WJMainItem *item = group.itemArray[indexPath.row];
    Class class = NSClassFromString(item.controller);
    UIViewController *controller = [[class alloc]init];
    controller.navigationItem.title = item.title;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma headerView的代理方法
-(void)groupViewDidClick:(WJMainGroupView *)groupView {
    [self.tableView reloadData];
}


@end
